package com.lovetemperature.action;

import com.lovetemperature.model.Member;
import com.lovetemperature.model.Staff;
import com.lovetemperature.model.StaffStatus;
import com.lovetemperature.service.MemberService;
import com.lovetemperature.service.StaffService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{
	private String account;
	private String password;
	private String message;
	private String result;
	
	private MemberService memberService;
	private StaffService staffService;

	@Override
	public String execute() throws Exception {
		Member member=null;
		Staff staff=null;
		if(account==null || password==null || account.equalsIgnoreCase("") || password.equalsIgnoreCase("")){
			message = "用户名或密码不能为空";
			result = "fail";
		}else if((member = memberService.login(account, password)) != null){
			ActionContext.getContext().getSession().put("user", member);
			message = "会员登录成功";
			result = "memberSuccess";
		}else if((staff = staffService.login(account, password))!=null){
			ActionContext.getContext().getSession().put("user", staff);
			message = "员工登录成功";
			if(staff.getStatus().equals(StaffStatus.总经理)){
				result = "managerSuccess";
			}else if(staff.getStatus().equals(StaffStatus.管理员)){
				result = "adminSuccess";
			}else{
				result = "staffSuccess";
			}
		}else {
			message = "用户名或密码错误";
			result = "fail";
		}
		return "json";
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public StaffService getStaffService() {
		return staffService;
	}

	public void setStaffService(StaffService staffService) {
		this.staffService = staffService;
	}
}
